package cn.itcast.service.system.impl;

import cn.itcast.dao.system.RoleDao;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService{

    @Autowired
    private RoleDao roleDao;

    @Override
    public void save(Role role) {
        roleDao.save(role);
    }

    @Override
    public void update(Role role) {
        roleDao.updateById(role);
    }

    @Override
    public void deleteById(String id) {
        roleDao.deleteById(id);
    }

    @Override
    public Role findById(String id) {

        return roleDao.findById(id);

    }

    @Override
    public PageInfo findPage(String companyId, int page, int size) {
        PageHelper.startPage(page,size);
        List<Role> list = roleDao.findAll(companyId);

        return new PageInfo(list);
    }

    @Override
    public List<Role> findAll(String companyId) {
        return  roleDao.findAll(companyId);
    }

    @Override
    public List<String> findRoleIdsByUserId(String userid) {
        return roleDao.findRoleIdsByUserId(userid);
    }

    @Override
    public void updateRoleModule(String roleid, String moduleIds) { //moduleIds "12,34,56,5634,23"
//         先删除这个角色下的权限数据
//        delete  from pe_role_module where role_id=?
        roleDao.deleteRoleAndModuleByRoleId(roleid);
//         再新增
        String[] mIds = moduleIds.split(",");
        for (String mId : mIds) {
            roleDao.saveRoleAndModule(roleid,mId);
        }
//        insert into pe_role_module(role_id,module_id) values (#{roleid},#{moduleid})



    }
}
